home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 18 / CU Amiga Magazine's Super CD-ROM 18 (1997)(EMAP Images)(GB)[!][issue 1998-01].iso / CUCD / Programming / AmigaE / Src / OOmodules / library / exec / port.e < prev   
Encoding:
Text File  |  1996-10-31  |  7.9 KB  |  434 lines

  1. OPT MODULE
  2.  
  3. MODULE  'oomodules/object',
  4.  
  5.         'exec/ports',
  6.         'exec/nodes',
  7.  
  8.         'gadtools'
  9.  
  10. EXPORT OBJECT port OF object
  11. /****** object/port ******************************
  12.  
  13.     NAME
  14.         port of object -- Message port object.
  15.  
  16.     FUNCTION
  17.  
  18.     ATTRIBUTES
  19.         mp:PTR TO mp -- Pointer to message port. Ready to use after new().
  20.  
  21.         lastMessage:PTR TO mn -- Pointer to last message received. The usual
  22.             restrictions about messages apply here, too -- don't access it
  23.             when you've replied.
  24.  
  25.     SEE ALSO
  26.         object, exec.library system documentation
  27.  
  28. ********/
  29.   mp:PTR TO mp
  30.   lastMessage:PTR TO mn
  31. ENDOBJECT
  32.  
  33. PROC init() OF port
  34. /****** port/init ******************************
  35.  
  36.     NAME
  37.         init() of port -- Initialization of the object.
  38.  
  39.     SYNOPSIS
  40.         port.init()
  41.  
  42.     FUNCTION
  43.         Creates an unnamed message port. Note that the name can be
  44.         set with the "name" tag in select().
  45.  
  46.     EXCEPTIONS
  47.         Raises "port", text when creation failed.
  48.  
  49.     EXAMPLE
  50.  
  51.        /*
  52.         * Add a port named 'testport' to the system
  53.         */
  54.  
  55.         NEW port.new(["name", 'testport', "add"])
  56.  
  57.  
  58.  
  59.     SEE ALSO
  60.         port
  61.  
  62. ********/
  63.  
  64.   self.mp := CreateMsgPort()
  65.  
  66.   IF self.mp = NIL THEN Throw("port",'Unable to create message port.')
  67.  
  68. ENDPROC
  69.  
  70. PROC select(opts,i) OF port
  71. /****** port/select ******************************
  72.  
  73.     NAME
  74.         select() of port -- Selection of action upon initialization.
  75.  
  76.     SYNOPSIS
  77.         port.select(LONG, LONG)
  78.  
  79.         port.select(opts, i)
  80.  
  81.     FUNCTION
  82.         Recognized tags are:
  83.             "name" -- set port's name
  84.             "add" -- add the port to the system
  85.  
  86.     INPUTS
  87.         opts:LONG -- List of options
  88.  
  89.         i:LONG -- Index of option list
  90.  
  91.     SEE ALSO
  92.         port
  93.  
  94. ********/
  95. DEF item
  96.  
  97.   item:=ListItem(opts,i)
  98.  
  99.  
  100.   SELECT item
  101.  
  102.     CASE "name"
  103.  
  104.       INC i
  105.       self.mp::ln.name := ListItem(opts,i)
  106.  
  107.     CASE "add"
  108.  
  109.       self.addToSystem()
  110.  
  111.   ENDSELECT
  112.  
  113. ENDPROC i
  114.  
  115. PROC getSignalMask() OF port IS Shl(1,self.mp.sigbit)
  116. /****** port/getSignalMask ******************************
  117.  
  118.     NAME
  119.         getSignalMask() of port -- Get signal mask for Wait()
  120.  
  121.     SYNOPSIS
  122.         port.getSignalMask()
  123.  
  124.     FUNCTION
  125.         Gets the signal mask from an existing port. This mask is used
  126.         in Wait().
  127.  
  128.     RESULT
  129.         LONG -- Signal mask of the port.
  130.  
  131.     EXAMPLE
  132.  
  133.        /*
  134.         * Wait for a message to arrive
  135.         */
  136.  
  137.         mask := port.getSignalMask()
  138.         port.waitForSignalMask()
  139.  
  140.     SEE ALSO
  141.         port, exec/Wait(), waitForSignalMask()
  142.  
  143. ********/
  144.  
  145. PROC getMsg(mode="exec") OF port
  146. /****** port/getMsg ******************************
  147.  
  148.     NAME
  149.         getMsg() of port -- Get message from port.
  150.  
  151.     SYNOPSIS
  152.         port.getMsg(LONG="exec")
  153.  
  154.         port.getMsg(mode)
  155.  
  156.     FUNCTION
  157.         Get a message from the port. This message can either be one of
  158.         exec's or a gadtools message, the only argument sets this mode.
  159.  
  160.     INPUTS
  161.         mode:LONG -- "exec" for exec, "gadt" for gadtools message. Default
  162.             is "exec"
  163.  
  164.     RESULT
  165.         PTR TO mn -- Message pointer or NIL if no message was at the port.
  166.  
  167.     EXAMPLE
  168.  
  169.        /*
  170.         * Wait for a message to arrive at exec's port.
  171.         */
  172.  
  173.         mask := port.getSignalMask()
  174.         IF (port.waitForSignalMask() AND mask)
  175.  
  176.          /*
  177.           * Get it.
  178.           */
  179.  
  180.           message := port.getMsg()
  181.  
  182.         ENDIF
  183.  
  184.     SEE ALSO
  185.         port, getSignalMask(), exec/Wait()
  186.  
  187. ********/
  188.  
  189.   SELECT mode
  190.  
  191.     CASE "gadt"
  192.  
  193.       IF gadtoolsbase
  194.  
  195.         self.lastMessage := Gt_GetIMsg(self.mp)
  196.         RETURN self.lastMessage
  197.  
  198.       ENDIF
  199.  
  200.     DEFAULT
  201.  
  202.       self.lastMessage := GetMsg(self.mp)
  203.       RETURN self.lastMessage
  204.  
  205.   ENDSELECT
  206.  
  207. ENDPROC
  208.  
  209. PROC replyMsg(mode="exec") OF port
  210. /****** port/replyMsg ******************************
  211.  
  212.     NAME
  213.         replyMsg() of port -- Reply a message.
  214.  
  215.     SYNOPSIS
  216.         port.replyMsg(LONG="exec")
  217.  
  218.         port.replyMsg(mode)
  219.  
  220.     FUNCTION
  221.         Reply a message that was received at the port. For multi-tasking
  222.         reasons this has to be done as soon as possible after receiving it.
  223.  
  224.     INPUTS
  225.         mode:LONG -- "exec" for exec message or "gadt" for gadtools.
  226.  
  227.     EXAMPLE
  228.  
  229.        /*
  230.         * Wait for a message to arrive at exec's port.
  231.         */
  232.  
  233.         mask := port.getSignalMask()
  234.         IF (port.waitForSignalMask() AND mask)
  235.  
  236.  
  237.  
  238.          /*
  239.           * Get it.
  240.           */
  241.  
  242.           message := port.getMsg()
  243.  
  244.  
  245.  
  246.          /*
  247.           * Copy the important data so we can reply at once.
  248.           */
  249.  
  250.           port.replyMsg()
  251.  
  252.         ENDIF
  253.  
  254.  
  255.     SEE ALSO
  256.         port, exec/Wait(), exec.library system documentation about messages
  257.  
  258. ********/
  259.  
  260.   IF self.lastMessage = NIL THEN RETURN
  261.  
  262.   SELECT mode
  263.  
  264.     CASE "gadt"
  265.  
  266.       IF gadtoolsbase THEN Gt_ReplyIMsg(self.lastMessage)
  267.  
  268.     DEFAULT
  269.  
  270.       ReplyMsg(self.lastMessage)
  271.  
  272.   ENDSELECT
  273.  
  274. ENDPROC
  275.  
  276. PROC waitForSignalMask(mask) OF port IS Wait(mask)
  277. /****** port/waitForSignalMask ******************************
  278.  
  279.     NAME
  280.         waitForSignalMask() of port -- Wait for signal mask.
  281.  
  282.     SYNOPSIS
  283.         port.waitForSignalMask(LONG)
  284.  
  285.         port.waitForSignalMask(mask)
  286.  
  287.     FUNCTION
  288.         Wait until a signal according to the mask is sent to the port.
  289.  
  290.     INPUTS
  291.         mask:LONG -- Mask to wait for. The should be the one from
  292.             getSignalMask().
  293.  
  294.     RESULT
  295.         LONG -- Signal mask received.
  296.  
  297.     SEE ALSO
  298.         port, getSignalMask()
  299.  
  300. ********/
  301.  
  302. PROC wait() OF port IS Wait(self.getSignalMask())
  303. /****** port/wait ******************************
  304.  
  305.     NAME
  306.         wait() of port -- Wait for a message to arrive.
  307.  
  308.     SYNOPSIS
  309.         port.wait()
  310.  
  311.     FUNCTION
  312.         Waits until a message arrives at the port. Use this function when
  313.         there is only one port in your program.
  314.  
  315.     RESULT
  316.         LONG -- Signal mask received.
  317.  
  318.     NOTES
  319.         See portList for waiting on any number of ports.
  320.  
  321.     SEE ALSO
  322.         port, portList
  323.  
  324. ********/
  325.  
  326. PROC end() OF port
  327. /****** port/end ******************************
  328.  
  329.     NAME
  330.         end() of port -- Global destructor.
  331.  
  332.     SYNOPSIS
  333.         port.end()
  334.  
  335.     FUNCTION
  336.         Removes the message port from the system and deletes it. Be sure you
  337.         have received all your messages.
  338.  
  339.     SEE ALSO
  340.         port
  341.  
  342. ********/
  343.  
  344.   IF self.mp = NIL THEN RETURN
  345.  
  346.   self.removeFromSystem()
  347.   DeleteMsgPort(self.mp)
  348.  
  349. ENDPROC
  350.  
  351. PROC addToSystem() OF port
  352. /****** port/addToSystem ******************************
  353.  
  354.     NAME
  355.         addToSystem() of port -- Add port to the system.
  356.  
  357.     SYNOPSIS
  358.         port.addToSystem()
  359.  
  360.     FUNCTION
  361.         Adds the port to the system, i. e. you can receive messages from
  362.         other programs if they know the name of your port. Provide the
  363.         "add" tag when newing the port object and it will be added when
  364.         it's initialized.
  365.  
  366.     SEE ALSO
  367.         port
  368.  
  369. ********/
  370.  
  371.   AddPort(self.mp)
  372.  
  373. ENDPROC
  374.  
  375. PROC removeFromSystem() OF port
  376. /****** port/removeFromSystem ******************************
  377.  
  378.     NAME
  379.         removeFromSystem() of port -- Remove port.
  380.  
  381.     SYNOPSIS
  382.         port.removeFromSystem()
  383.  
  384.     FUNCTION
  385.         Removes the port from the system. No other task will be able to send
  386.         you messages any more. Called in END automatically, DO NOT call it
  387.         unless you know what you do.
  388.  
  389.     SEE ALSO
  390.         port
  391.  
  392. ********/
  393.  
  394.   RemPort(self.mp)
  395.  
  396. ENDPROC
  397.  
  398. EXPORT PROC wrapPort(p:PTR TO mp)
  399. /****** port/wrapPort ******************************
  400.  
  401.     NAME
  402.         wrapPort() -- Wrap a port system structure in an object.
  403.  
  404.     SYNOPSIS
  405.         wrapPort(PTR TO mp)
  406.  
  407.         wrapPort(p)
  408.  
  409.     FUNCTION
  410.         Wraps the system structure port in an object of that type. When
  411.         you have a port from, say, a window this can be quite useful. The
  412.         window port can then be used as if it was a normal port object.
  413.  
  414.     INPUTS
  415.         p:PTR TO mp -- Port to wrap.
  416.  
  417.     RESULT
  418.         PTR TO port -- Port object that was just created.
  419.  
  420.     SEE ALSO
  421.         portList
  422.  
  423. ********/
  424. DEF port:PTR TO port
  425.  
  426.   NEW port.new()
  427.   DeleteMsgPort(port.mp)
  428.   port.mp := p
  429.  
  430.   RETURN port
  431.  
  432. ENDPROC
  433.  
  434.